const books = wx.cloud.database().collection("books")
Page({
    data: {
        list: [],
        imageUrl:""
    },
    async onLoad() {
        let result = await books.where({ name: "西游记" }).get()
        this.setData({
            list: result.data
        })

    },
    async add() {
        //在小程序端操作数据库
        try {
            let result = await books.add({
                data: { name: "聊斋志异", author: "蒲松龄" }
            })
        } catch (error) {
            console.log(error)
        }

    },
    //删除
    async remove() {

        try {
            //删除某一个
            // let result = await books.doc("fc8e6465641114ff04816147043e7a7b").remove()
            // console.log(result)

            //批量删除
            let result = await books.where({ author: "吴承恩" }).remove()
            console.log(result)
        } catch (error) {

        }

    },
    async update() {
        try {
            //更新某一个
            // let result = await books.doc("4ae805bd6410247300023fee374fef7d").update({
            //     data: { name: "新三国演义" }
            // })
            // console.log(result)

            //批量更新
            let result = await books.where({ author: "蒲松龄" }).update({
                data: { name: "新聊斋志异" }
            })
            console.log(result)

        } catch (error) {

        }

    },
    //上传文件
    async upload() {

        //上传
        try {
            let result = await wx.chooseMedia()

            let temPath = result.tempFiles[0].tempFilePath
            let ext = /\.[^\.]\w*$/.exec(temPath)[0]

            result = await wx.cloud.uploadFile({
                cloudPath: "test/" + new Date().getTime() + ext,
                filePath: temPath
            })
            // console.log(result)
            this.setData({
                imageUrl: result.fileID
            })
        } catch (error) {
            console.log(error)
        }


    }


})